package com.xt.LibaryDeskManager.servlet;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xt.LibaryDeskManager.dto.TeacherDto;
import com.xt.LibaryDeskManager.dto.UpdateSeatTeacher;
import com.xt.LibaryDeskManager.service.TeacherService;

import com.xt.LibaryDeskManager.utils.StrUtils;
import com.xt.LibaryDeskManager.vo.TeacherVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    /**
     * 获得教师数量
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/getCount")
    public Map getCount() throws Exception{
        int count=teacherService.getCount();
        return StrUtils.creatMap("count",count);
    }

    /**
     * 分页查询教师信息
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/allteacher")
    public Object allteacher(String type,Integer page,Integer limit) throws Exception {
        if ("updateSeat".equals(type)){
            List<UpdateSeatTeacher> list1=new ArrayList<>();
            try {
                list1=teacherService.getTeacherNoName();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return list1;
        }else{
            Page page1=PageHelper.startPage(page,limit,true);
            List<TeacherDto> list1=teacherService.getAllTeacher();
            return StrUtils.creatMap(list1,page1.getTotal());
        }
    }

    /**
     * 根据工号删除教师
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/deleteTeacher")
    public Map deleteTeacher (Integer teacherNo) throws Exception{
        int code= teacherService.deleteTeacher(teacherNo);
        return StrUtils.creatMessage("code",code);
    }

    /**
     * 添加教师信息
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/addTeacher")
    public Map addTeacher(TeacherVo teacherVo) throws Exception{
        int code= teacherService.addTeacher(teacherVo);
        return StrUtils.creatMessage("code",code);
    }
    /**
     * 更新教师信息
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/updateTeacher")
    public Map updateTeacher (TeacherVo teacherVo) throws Exception{
        int code= teacherService.updateTeacher(teacherVo);
        return StrUtils.creatMessage("code",code);
    }
}
